﻿#include "IntelligentCreatureData.h"

IntelligentCreatureRules::IntelligentCreatureRules()
{
}

IntelligentCreatureRules::IntelligentCreatureRules(BinaryReader *reader)
{
    MinimalFullness = reader->ReadInt32();
    MousePickupEnabled = reader->ReadBoolean();
    MousePickupRange = reader->ReadInt32();
    AutoPickupEnabled = reader->ReadBoolean();
    AutoPickupRange = reader->ReadInt32();
    SemiAutoPickupEnabled = reader->ReadBoolean();
    SemiAutoPickupRange = reader->ReadInt32();

    CanProduceBlackStone = reader->ReadBoolean();
}

void IntelligentCreatureRules::Save(BinaryWriter *writer)
{
    writer->Write(MinimalFullness);
    writer->Write(MousePickupEnabled);
    writer->Write(MousePickupRange);
    writer->Write(AutoPickupEnabled);
    writer->Write(AutoPickupRange);
    writer->Write(SemiAutoPickupEnabled);
    writer->Write(SemiAutoPickupRange);

    writer->Write(CanProduceBlackStone);
}

IntelligentCreatureItemFilter::IntelligentCreatureItemFilter()
{
}

void IntelligentCreatureItemFilter::SetItemFilter(int idx)
{
    switch (idx)
    {
        case 0: //all items
            PetPickupAll = true;
            PetPickupGold = false;
            PetPickupWeapons = false;
            PetPickupArmours = false;
            PetPickupHelmets = false;
            PetPickupBoots = false;
            PetPickupBelts = false;
            PetPickupAccessories = false;
            PetPickupOthers = false;
            break;
        case 1: //gold
            PetPickupAll = false;
            PetPickupGold = !PetPickupGold;
            break;
        case 2: //weapons
            PetPickupAll = false;
            PetPickupWeapons = !PetPickupWeapons;
            break;
        case 3: //armours
            PetPickupAll = false;
            PetPickupArmours = !PetPickupArmours;
            break;
        case 4: //helmets
            PetPickupAll = false;
            PetPickupHelmets = !PetPickupHelmets;
            break;
        case 5: //boots
            PetPickupAll = false;
            PetPickupBoots = !PetPickupBoots;
            break;
        case 6: //belts
            PetPickupAll = false;
            PetPickupBelts = !PetPickupBelts;
            break;
        case 7: //jewelry
            PetPickupAll = false;
            PetPickupAccessories = !PetPickupAccessories;
            break;
        case 8: //others
            PetPickupAll = false;
            PetPickupOthers = !PetPickupOthers;
            break;
    }
    if (PetPickupGold && PetPickupWeapons && PetPickupArmours && PetPickupHelmets && PetPickupBoots && PetPickupBelts && PetPickupAccessories && PetPickupOthers)
    {
        PetPickupAll = true;
        PetPickupGold = false;
        PetPickupWeapons = false;
        PetPickupArmours = false;
        PetPickupHelmets = false;
        PetPickupBoots = false;
        PetPickupBelts = false;
        PetPickupAccessories = false;
        PetPickupOthers = false;
    }
    else
    {
        if (!PetPickupGold && !PetPickupWeapons && !PetPickupArmours && !PetPickupHelmets && !PetPickupBoots && !PetPickupBelts && !PetPickupAccessories && !PetPickupOthers)
        {
        PetPickupAll = true;
        }
    }
}

IntelligentCreatureItemFilter::IntelligentCreatureItemFilter(BinaryReader *reader)
{
    PetPickupAll = reader->ReadBoolean();
    PetPickupGold = reader->ReadBoolean();
    PetPickupWeapons = reader->ReadBoolean();
    PetPickupArmours = reader->ReadBoolean();
    PetPickupHelmets = reader->ReadBoolean();
    PetPickupBoots = reader->ReadBoolean();
    PetPickupBelts = reader->ReadBoolean();
    PetPickupAccessories = reader->ReadBoolean();
    PetPickupOthers = reader->ReadBoolean();
    //PickupGrade = (ItemGrade)reader.ReadByte();
}

void IntelligentCreatureItemFilter::Save(BinaryWriter *writer)
{
    writer->Write(PetPickupAll);
    writer->Write(PetPickupGold);
    writer->Write(PetPickupWeapons);
    writer->Write(PetPickupArmours);
    writer->Write(PetPickupHelmets);
    writer->Write(PetPickupBoots);
    writer->Write(PetPickupBelts);
    writer->Write(PetPickupAccessories);
    writer->Write(PetPickupOthers);
    //writer.Write((byte)PickupGrade);
}

ClientIntelligentCreature::ClientIntelligentCreature()
{
}

ClientIntelligentCreature::ClientIntelligentCreature(BinaryReader *reader)
{
    PetType = static_cast<IntelligentCreatureType>(reader->ReadByte());
    Icon = reader->ReadInt32();

    CustomName = reader->ReadString();
    Fullness = reader->ReadInt32();
    SlotIndex = reader->ReadInt32();
    Expire = DateTime::FromBinary(reader->ReadInt64());
    BlackstoneTime = reader->ReadInt64();

    petMode = static_cast<IntelligentCreaturePickupMode>(reader->ReadByte());

    CreatureRules = new IntelligentCreatureRules(reader);
    Filter = new IntelligentCreatureItemFilter(reader);
    Filter->PickupGrade = static_cast<ItemGrade>(reader->ReadByte());
    MaintainFoodTime = reader->ReadInt64();
}

void ClientIntelligentCreature::Save(BinaryWriter *writer)
{
    writer->Write(static_cast<unsigned char>(PetType));
    writer->Write(Icon);

    writer->Write(CustomName);
    writer->Write(Fullness);
    writer->Write(SlotIndex);
    writer->Write(Expire.ToBinary());
    writer->Write(BlackstoneTime);

    writer->Write(static_cast<unsigned char>(petMode));

    CreatureRules->Save(writer);
    Filter->Save(writer);
    writer->Write(static_cast<unsigned char>(Filter->PickupGrade));
    writer->Write(MaintainFoodTime);
}
